package com.rhdtp.demopf.converter;

import com.rhdtp.demopf.model.dao.PeliculaDAO;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import com.rhdtp.demopf.model.dto.Pelicula;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PeliculaConverter implements Converter {

    private PeliculaDAO peliculaDAO;

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if (value != null && value.trim().length() > 0) {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            peliculaDAO = (PeliculaDAO) context.getBean("peliculaDAO");
            return peliculaDAO.list().get(Integer.parseInt(value));
        } else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object != null) {
            return String.valueOf(((Pelicula) object).getId());
        } else {
            return null;
        }
    }

    public PeliculaDAO getPeliculaDAO() {
        return peliculaDAO;
    }

    public void setPeliculaDAO(PeliculaDAO peliculaDAO) {
        this.peliculaDAO = peliculaDAO;
    }
}
